<template>
<div class="content feedback">
  <header class="bar bar-nav">
    <a class="button button-link button-nav pull-left" v-link="{path: '/home', replace: true}">
    <span class="icon icon-left"></span>
    </a>
    <h1 class="title" v-text="title"></h1>
  </header>

 <validator name="validation1">
  <div class="content-block">
    <div class="feedback-label">
          <div >
            <div >
	      <span style="color:red">*</span> 问题类别：
              <select v-model="type">
                <option value='1'>留学申请</option>
                <option value='2'>房屋租赁</option>
		<option value='3'>旅游</opttion>
              </select>
            </div>
          </div>
        </div>
    <div class="feedback-label">
      <span style="color:red">*</span> 问题描述：
    </div>
    <div class="item-input">
      <textarea rows="7" placeholder="请输入您的问题，不得为空" @invalid="infoInvalid" v-validate:content="['required']" detect-change="off" initial='off' v-model="content"></textarea>
    </div>
    <div class="feedback-label">
      <span style="color:red">*</span>请留下您的联系电话或邮箱，谢谢！
    </div>
    <div class="feedback-input">
      <input type="text" placeholder="电话" @invalid="phoneInvalid" v-validate:phone="['tel']" detect-change="off" initial='off' v-model="phone">
      <input type="text" placeholder="邮箱" @invalid="emailInvalid" v-validate:email="['email']" detect-change="off" initial='off' v-model="email">
    </div>
  </div>
   <div class="content-block">
    <div class="row">
      <div class="col-100"><a class="button button-big  button-success" style="background:#4cd964;color:white;border:none;" v-link="{path: '/home'}" @click="doflag($validation1.invalid)">提交</a></div>
    </div>
   </div>
  </validator>
</div>
</template>

<script>
import $ from 'zepto'
import validator from '../validation'

export default {
  route: {
    canDeactivate (transition) {
      if (this.flag) {
        console.log('if.....')
        transition.abort()
        $.toast('请检查输入是否正确')
        this.flag = false
      }
      else {
        console.log('else.....')
        this.$http.post(this.apiUrl + this.type, {content: this.content, phone: this.phone, email: this.email}).
        then(function (response) {
          if (response.json().ok) {
            $.toast('提交成功')
          }
        })
        transition.next()
      }
    }
  },
  ready () {
  },
  data () {
    return {
      title: '问题咨询',
      type: 1,
      apiUrl: 'http://creatgo.com/oec/consult?method=email&to=',
      flag: false,
      content: '',
      phone: '',
      email: ''
    }
  },
  components: {
    validator
  },
  methods: {
    phoneInvalid () {
      $.toast('手机号码错误')
    },
    emailInvalid () {
      $.toast('邮箱格式错误')
    },
    infoInvalid () {
      $.toast('不能为空')
    },
    doflag (invalid) {
      this.flag = invalid
    }
  }
}
</script>

<style>
.sub-title {
  margin-top: 2.2rem;
  background-color: white;
  height: 2.2rem;
  font-size: .8rem;
  font-weight: normal;
  line-height: 2.2rem;
  text-align: center;
  border-bottom: 1px solid #d8d8d9;
}
.feedback .content-block {
  margin: 2.3rem 0 0 0;
  padding: 0 .45rem;
}
.feedback-label {
  font-size: .7rem;
  line-height: 1.2rem;
}
.feedback-input input{
  width: 100%;
  height: 2.15rem;
  font-size: .7rem;
  padding: .4rem .5rem;
  background-color: #fff;
  margin-bottom: .3rem;
  border: 1px solid rgba(0,0,0,.2);
  border-radius: .2rem;
}
.feedback textarea {
  height: auto;
  resize: none;
  width: 100%;
  font-size: .7rem;
  -webkit-appearance: none;
  padding: .4rem .5rem;
  -webkit-user-select: text;
  background-color: #fff;
  border: 1px solid rgba(0,0,0,.2);
  border-radius: .2rem;
  outline: 0;
}
.feedback .submit-button {
  margin-top: .8rem;
  width: 100%;
  padding: 0 .45rem;
}
.feedback .submit-button button {
  background-color: #ed8e07;
  width: 100%;
  line-height: 2.1rem !important;
  height: 2.1rem !important;
}
</style>